// components/commodity_info/commodity_info.js
let backupsShareInfo = {}; // 父组件的分享信息
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        isShow:false, //是否显示
        showType:0, // 0=>加入购物车1 1=>加入购物车2
        commodity:null,// 商品信息
    },

    /**
     * 组件的方法列表
     */
    methods: {
        setCommodityInfo(obj) {// 父组件调用
          const item = JSON.parse(JSON.stringify(obj));
          let showType = false
          if (item.payNumber > 0) {
            showType = true
          } else {
            item.payNumber = 1
          }
          this.setData({ commodity: item, showType, isShow: true });
          this.shareInfoEvent(this.getShareInfo(), 1); // 分享参数回传: 设置
        },
        addCommNumber(){
          let commodity = this.data.commodity;
          commodity.payNumber++;
          this.setData({commodity: commodity});
        },
        reduceCommNumber(){
          let commodity = this.data.commodity;
          if(commodity.payNumber <= 1){return};
          commodity.payNumber--;
          this.setData({commodity: commodity});
        },
        settingCommodityNumber(){
          const commodity = this.data.commodity;
          this.closeCommodity();
          this.triggerEvent('settingCommodityNumber',{cid:commodity.cid,payNumber:commodity.payNumber});
        },
        // 备份父组件的分享信息
        backupsShareInfos(binfo){
          backupsShareInfo = binfo; // 备份
        },
        // 显示商品详情: 可操作
        showCommodity(){
          this.setData({showType: true});
        },
        // 组件回调
        sbgTapCallback(){
          this.closeCommodity();
        },
        closec(){
          this.closeCommodity();
        },
        closeCommodity(){
          this.setData({isShow:!this.data.isShow});
          this.shareInfoEvent(backupsShareInfo,0); // 分享参数回传: 还原
        },
        getShareInfo(){
          const commodity = this.data.commodity;
          const tempObj = {
            title: '好吃的零食-'+commodity.cName,
            imageUrl: commodity.imageUrl,
            desc: '原价需要:'+commodity.oPrice+'元, 现在只需要:'+commodity.price+'元哦~~',
            path: '/pages/index/index?cid='+commodity.cid
           };
          return tempObj;
        },
        shareInfoEvent(pObj,type){
          this.triggerEvent('shareInfo',{pObj:pObj,type:type});
        }
    }
})
